package dm;

import java.sql.CallableStatement;
import java.sql.Connection;

import database.DBConnection;

public class ShoppingDM {

	public void addProduct(int id, int count,int userId) throws Exception {
		
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;

		try {
			statement = con.prepareCall("{call PRODUCT_SELL_PKG.ADD_PRO_SELL(?,?,?)}");
			statement.setInt(1,id);
			statement.setInt(2, count);
			statement.setInt(3,userId);
			statement.execute();
			
			statement.close();
			DBConnection.closeConnection(con);
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
		
	}

	public void removeProduct(int id, int userId) throws Exception {
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;

		try {
				statement = con.prepareCall("{call PRODUCT_SELL_PKG.REMOVE_PRO_SELL(?,?)}");
				statement.setInt(1,id);
				statement.setInt(2,userId);
				statement.execute();
				statement.close();
				
				DBConnection.closeConnection(con);
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
}
	
	public void updateProduct(int id, int count, int userId) throws Exception{
		Connection con = DBConnection.getConnection();
		CallableStatement statement = null;

		try {
				statement = con.prepareCall("{call PRODUCT_SELL_PKG.UPDATE_PRO_SELL(?,?,?)}");
				statement.setInt(1,id);
				statement.setInt(2, count);
				statement.setInt(3,userId);
				statement.execute();
				statement.close();
				
				DBConnection.closeConnection(con);
		} catch (Exception e) {
			try {
				statement.close();
			} catch (Exception exception) {
			}
			try {
				DBConnection.closeConnection(con);
			} catch (Exception exception) {
			}
			throw e;
		}
	}

}
